<template>
  <div>
    <br/>
    <el-menu
        class="el-menu-demo"
        mode="horizontal"
        background-color="#545c64"
        text-color="#fff"
        active-text-color="#ffd04b">
      <el-menu-item index="1">{{ nickName }}</el-menu-item>
      <el-menu-item index="1">
        <el-badge :value="number" class="item">
          <a href="#/showCart">我的购物车</a>
        </el-badge>
      </el-menu-item>
      <el-menu-item index="1">
        <a href="javascript:;" @click="logout">注销</a>
      </el-menu-item>
    </el-menu>

    <el-table
        :data="goods"
        style="width: 100%">
      <el-table-column
          prop="goodsCode"
          label="编号"
          width="180">
      </el-table-column>
      <el-table-column
          prop="goodsName"
          label="名称"
          width="180">
      </el-table-column>
      <el-table-column
          prop="goodsPrice"
          label="价格">
      </el-table-column>
      <el-table-column
          prop="provider.data.proName"
          label="供应商">
      </el-table-column>
      <el-table-column
          fixed="right"
          label="操作"
          width="100">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="addCart(scope.row)">加入购物车</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import axios from "axios";

export default {
  name: "Index",
  data() {
    return {
      goods: [],
      nickName: window.localStorage.getItem('nickName'),
      number:0
    }
  },
  created() {
    this.loadAllGoods()
  },
  methods: {
    loadAllGoods() {
      let _this = this
      axios.get('/api/smbms-goods/goods', {
        headers: {
          token: window.localStorage.getItem('token')
        }
      }).then((response) => {
        _this.goods = response.data.data
      }).catch(() => {
        _this.$router.push('login')
      })
    },
    addCart(row) {
      let _this = this
      axios.get('/api/smbms-goods/goods/addcart', {
        headers: {
          token: window.localStorage.getItem('token')
        },
        params: {
          id: row.id
        }
      }).then((response) => {
        this.number++
        this.$message({
          message: row.goodsName + '加入购物车成功！',
          type: 'success'
        });
      }).catch(() => {
        _this.$router.push('login')
      })
    },
    showCart() {
      this.$router.push('showCart')
    }
  }
}
</script>

<style scoped>

</style>